<template>
  <view class="container">
    <view class="list-container">
      <view v-if="recordList.length === 0 && status !== 'loading'" class="empty-state"> {{$t('acupoint.empty')}} </view>

      <view v-else class="record-card" v-for="item in recordList" :key="item.id" @click="goToDetail(item.face_id)">
        <view class="card-left">
          <text class="result-text">{{ item.complexion_base_explain }}</text>

          <text class="date-text">{{ item.create_datetime }}</text>
        </view>
        <!-- <view v-if="currentTab === 'tongue'" class="card-right">
          <view class="score-circle" :style="getCircleStyle(item.medicine_total)">
            <text class="score-text">{{ item.medicine_total }}</text>
          </view>
        </view> -->
      </view>

      <view class="custom-load-more">
        <view v-if="status === 'loading'">
          <text class="load-more-text">{{$t('acupoint.loading')}}</text>
        </view>
        <view v-if="status === 'noMore'">
          <text class="load-more-text">—— {{$t('acupoint.no_more')}} ——</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { faceDiagnosisList, tonguetList } from '@/api/acupoint.js'
export default {
  data() {
    return {
      currentTab: 'tongue', // 'tongue': 舌诊, face: 面诊
      recordList: [],
      page: 1,
      pageSize: 10,
      status: 'more' // 'more', 'loading', 'noMore'
    }
  },
  onLoad() {
    this.getList()
    this.setTitle()
  },
  onReachBottom() {
    if (this.status === 'more') {
      this.getList()
    }
  },
  onPullDownRefresh() {
    this.resetAndReload()
  },
  methods: {
    setTitle() {
      // 国际化页面标题
      if (this.$t) {
        uni.setNavigationBarTitle({
          title: this.$t('faceDiagnosis.title')
        })
      }
    },
    changeTab(name) {
      if (this.currentTab === name) return
      this.currentTab = name
      this.resetAndReload()
    },
    resetAndReload() {
      this.page = 1
      this.recordList = []
      this.status = 'more'
      this.getList()
    },
    async getList() {
      if (this.status !== 'more') return
      this.status = 'loading'

      try {
        const params = {
          page: this.page,
          pageSize: this.pageSize,
          part_name: this.currentTab === 'tongue' ? '舌' : '脸部'
        }

        const res = await faceDiagnosisList(params)

        if (res.code === 200 && res.success) {
          const newList = res.result.items || []

          this.recordList = [...this.recordList, ...newList]

          if (newList.length < this.pageSize) {
            this.status = 'noMore'
          } else {
            this.status = 'more'
            this.page++
          }
        } else {
          uni.showToast({ title: res.message || '数据加载失败', icon: 'none' })
          this.status = 'more'
        }
      } catch (error) {
        console.error('API Error:', error)
        uni.showToast({ title: '网络错误，请稍后重试', icon: 'none' })
        this.status = 'more'
      } finally {
        uni.stopPullDownRefresh()
      }
    },
    getCircleStyle(score) {
      if (!score) score = 0 // 安全校验
      const angle = (score / 100) * 360
      return {
        background: `conic-gradient(#6C5FF8 ${angle}deg, #E6E6FA 0deg)`
      }
    },
    goToDetail(key) {
      uni.navigateTo({
        url: `/pages/record/detail/index?id=${key}&source=face`
      })
    }
  }
}
</script>
<style lang="scss">
page {
  background-color: #f5f5f5;
}

.container {
  display: flex;
  flex-direction: column;
}

/* Tab 样式 */
.tabs-container {
  position: sticky;
  top: 0;
  left: 0;
  width: 100%;
  display: flex;
  background-color: #ffffff;
  z-index: 99;
  border-bottom: 1px solid #f0f0f0;
  /* #ifdef H5 */
  // H5中，原生导航栏会占据空间，需要调整top值
  top: var(--window-top);
  /* #endif */

  .tab-item {
    flex: 1;
    text-align: center;
    padding: 24rpx 0;
    font-size: 30rpx;
    color: #666;
    position: relative;
    transition: color 0.3s;

    &.active {
      color: #333;
      font-weight: bold;

      &::after {
        content: "";
        position: absolute;
        bottom: 10rpx;
        left: 50%;
        transform: translateX(-50%);
        width: 60rpx;
        height: 6rpx;
        background-color: #6c5ff8; // 主题紫色
        border-radius: 3rpx;
      }
    }
  }
}

/* 列表样式 */
.list-container {
  padding: 24rpx;
}

.record-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #ffffff;
  border-radius: 16rpx;
  padding: 32rpx;
  margin-bottom: 24rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.04);
}

.card-left {
  flex: 1;
  display: flex;
  flex-direction: column;
  margin-right: 30rpx;

  .result-text {
    font-size: 28rpx;
    color: #333333;
    line-height: 1.6;
    margin-bottom: 16rpx;

    // display: -webkit-box;
    // -webkit-box-orient: vertical;
    // -webkit-line-clamp: 3;
    // overflow: hidden;
  }

  .date-text {
    font-size: 24rpx;
    color: #999999;
  }
}

.card-right {
  .score-circle {
    width: 100rpx;
    height: 100rpx;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;

    &::before {
      content: "";
      position: absolute;
      width: 80rpx;
      height: 80rpx;
      background: #ffffff;
      border-radius: 50%;
    }
  }

  .score-text {
    font-size: 32rpx;
    color: #333;
    font-weight: bold;
    position: relative;
    z-index: 1;
  }
}

// 空状态
.empty-state {
  text-align: center;
  color: #999;
  padding-top: 200rpx;
  font-size: 28rpx;
}

.custom-load-more {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20rpx 0;

  .load-more-text {
    font-size: 28rpx;
    color: #999999;
  }
}
</style>
